﻿using INTEX.DataBase;
using System;
using System.Data;
namespace WPFExcelReport
{

    public partial class MainViewModel
    {
        /// <summary>
        /// 1.自动生产生产单所需要的必要数据
        /// 2.利用ICC_ABAX表明生产单之间的计划关系
        /// 3.产生原料需求单
        /// </summary>
        /// <param name="dwInput"></param>
        /// <param name="src"></param>
        /// <returns></returns>
        public DataSet PlanAB05(DataSet SetEnv, DataSet db, DataRow dwABA, DataRow dwRule)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tmpABPOS = db.Tables["ABPOS"].Clone();
            DataTable tmpWDECK = db.Tables["WDECK"].Clone();
            string filter = "";
            string ENDD = "";
            string STARTD = "";
            DataRow[] rows = null;
            DataRow dwART = null;
            string krs = "", lfd = "", pos = "";
            string ART = "";
            string VAR = "";
            string WAKZ = "";
            string FARBE = "";
            string FINISH = "";
            string UM = "米";
         
            DataRow dwAPZUORD = null;
            string APZANRKRS = "";
            string APZALFDNR = "";
            DataRow dwABPN = null;
            #region========Validate=================
            string LFD0 = dwABA["ABAAUFLFD"].ToString();
            if (LFD0.Trim() == "")
                LFD0 = ABPLFD;
            string POS0 = dwABA["ABAAUFPOS"].ToString();
            if (POS0.Trim() == "")
            {
                POS0 = ABPOS.ToString(); ABPOS++;
            }
            krs = dwABA["ABAVKAUFKRS"].ToString();
            lfd = dwABA["ABAVKAUFLFD"].ToString();
            pos = dwABA["ABAVKAUFPOS"].ToString();
            ART = dwABA["ABAART"].ToString();
            VAR = dwABA["ABAVAR"].ToString();
            WAKZ = dwABA["ABAWAKZ"].ToString();
            FARBE = dwABA["ABAFARBE"].ToString();
            FINISH = dwABA["ABABESCHICH"].ToString();

            filter = "WAART='" + ART + "'";
            rows = db.Tables["WASTAMM"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwART = row;
            }
            if (dwART == null)
            {
                throw new System.Exception("没有对应的工艺档案");
                return SReturn;
            }

            filter = "APZSTUFE='F' AND APZART='" + ART
                + "' AND APZVAR='" + VAR + "' ";
            rows = db.Tables["APZUORD"].Select(filter);//允许界面更改流程
            foreach (DataRow row in rows)
            {
                dwAPZUORD = row;
                break;
            }
            if (dwAPZUORD == null)
            {
                if (IsAB05)
                {
                    //throw new System.Exception("没有对应的工艺流程");
                }
                //return SReturn;
                APZANRKRS = "GY";
                APZALFDNR = "0001";
            }
            else
            {
                APZANRKRS = dwAPZUORD["APZANRKRS"].ToString();
                APZALFDNR = dwAPZUORD["APZALFDNR"].ToString();
            }
            #endregion
            #region==============规则================ 
            QTYEND = 0; QTYMAT = 0; R01 = 0; R02 = 0; F01 = 0;
            if (dwRule != null)
            {
                R01 = Convert.ToDouble(dwRule["R01"].ToString()) / 10000;//整理缩率
                R02 = Convert.ToDouble(dwRule["R02"].ToString()) / 100;//零次布
                F02 = Convert.ToDouble(dwRule["F02"].ToString());//成品可用
                DELDATE = dwRule["DELDATE"].ToString();//手工设定的交期
                QTYEND = Convert.ToDouble(dwRule["QTYEND"].ToString());
                LDAB =Convert.ToDouble( dwRule["LEADTIME"].ToString());//生产周期
                
            }
            /*
            if (QTYEND < 0)
            {
                QTYEND = Math.Ceiling(Convert.ToDouble(dwABA["ABARESMG"].ToString()) / 10 * 0.01);//无放大系数
            }
            else
            {
                QTYEND = QTYEND;
            }*/
            QTYEND = Math.Ceiling(Convert.ToDouble(dwABA["ABARESMG"].ToString()) / 10 * 0.01);//无放大系数
            if (F02 > 0)
            {
                QTYEND = QTYEND - F02;
            }
            if (DELDATE.Trim().Length < 8)
            {
                ENDD = dwABA["ABAVKSTARTD"].ToString();
            }
            else
            {
                ENDD = DELDATE;
            }
            STARTD = Helpers.Helper.GetDateAddDays(ENDD, Convert.ToInt16(0 - LDAB));
            if (STARTD.Trim() == "") STARTD = "0"; 
     
            #endregion
            #region=================

           
            filter = "APFANRKRS='" + APZANRKRS + "' AND APFALFDNR='" + APZALFDNR + "'";
            rows = db.Tables["APFOLGE"].Select(filter);
            foreach (DataRow row in rows)
            {
                PRLO = row["KSZLO"].ToString();
                PRLB = row["KSZLB"].ToString();
                PRLP = row["KSZLP"].ToString();
                break;
            }

            double WARSTDLG = Convert.ToDouble(dwART["WARSTDLG"].ToString());
            double WALAEAEND = System.Convert.ToDouble(dwART["WALAEAEND"].ToString());
            UM = dwART["WAUEBM"].ToString();
            double WAFBRTL = System.Convert.ToDouble(dwART["WAFBRTL"].ToString());
            double WARGRLFD = System.Convert.ToDouble(dwART["WARGRLFD"].ToString());
            double WAFGRLFD = System.Convert.ToDouble(dwART["WAFGRLFD"].ToString());
            double WAFSTDLG = System.Convert.ToDouble(dwART["WAFSTDLG"].ToString());
            double WARWBRT = System.Convert.ToDouble(dwART["WARWBRT"].ToString());
            if (WAFSTDLG == 0)
            {
                WAFSTDLG = 100000;
            }
    
            QTYMAT =Math.Ceiling(QTYEND * (1 + R02)/(1-R01)); 
            //GLeng = System.Math.Round(FLeng / (1 + WALAEAEND * Convert.ToDecimal(0.0001)), 0);
          
            #endregion
            #region=======dwABPN==========
            dwABPN = tmpABPOS.NewRow();
            dwABPN["ABPAENDZL"] = 6;
            dwABPN["ABPMANDANT"] = app.Mandant;  
            dwABPN["ABPWAKZ"] = "F";
            dwABPN["ABPAUFART"] = "VA";
            dwABPN["ABPAUFKRS"] = "T6";//界面设定
            dwABPN["ABPAUFLFD"] = LFD0;
            dwABPN["ABPAUFPOS"] = POS0;
            dwABPN["ABPANRKR"] = APZANRKRS;
            dwABPN["ABPALFDNR"] = APZALFDNR;
            dwABPN["ABPSERIE"] = " ";
            dwABPN["ABPART"] = ART;
            dwABPN["ABPVAR"] = VAR;
            //dwABPN["ABPWAKZ"] = WAKZ;
            dwABPN["ABPFARBE"] = FARBE;
            dwABPN["ABPSME"] = 2;
            dwABPN["ABPLTVORL"] = dwABA["EXART"];
            dwABPN["ABPWAART"] = "   ";
            dwABPN["ABPBESCHICH"] = FINISH;

            dwABPN["ABPLIEFDAT"] = ENDD;

            dwABPN["ABPSTARTDAT"] = STARTD;

            dwABPN["ABPZUSATZ1"] = "F";
            dwABPN["ABPZUSATZ2"] = "B";
            dwABPN["ABPAESTAT"] = "J";

            dwABPN["ABPSTDLAE"] = Convert.ToDecimal(dwART["WARSTDLG"].ToString()) / 10;
            dwABPN["ABPMGV"] = 10;
            dwABPN["ABPMGB"] = 9999999;
            dwABPN["ABPZLO"] = PRLO;
            dwABPN["ABPZLB"] = PRLB;
            //dwABPN["ABPBLO"] = PRLO;
            dwABPN["ABPLAEAENDA"] = dwART["WALAEAEND"];

            dwABPN["ABPSOLLMENGE1"] = QTYMAT * 100;
            dwABPN["ABPSOLLMENGE2"] = QTYEND * 100;
            dwABPN["ABPSOLLMENGE3"] = 100 * Math.Ceiling(QTYMAT * WARWBRT * Convert.ToDouble(0.001));
            dwABPN["ABPSOLLMENGE4"] = 100 * Math.Ceiling(QTYEND * WAFBRTL * Convert.ToDouble(0.001));
            dwABPN["ABPSOLLMENGE5"] = 100 * Math.Ceiling(QTYMAT * WARGRLFD * Convert.ToDouble(0.001));
            dwABPN["ABPSOLLMENGE6"] = 100 * Math.Ceiling(QTYEND * WAFGRLFD * Convert.ToDouble(0.001));
            dwABPN["ABPSOLLMENGE7"] = 100 * Math.Ceiling(QTYEND / (WAFSTDLG * Convert.ToDouble(0.001)));
            dwABPN["ABPSOLLMENGE8"] = 100 * Math.Ceiling(QTYMAT / (WARSTDLG * Convert.ToDouble(0.001)));

            dwABPN["ABPNEUBEN"] = app.User;
            dwABPN["ABPNEUDAT"] = date; ;
            tmpABPOS.Rows.Add(dwABPN); 
            SReturn.Merge(tmpABPOS); 
            #endregion
            #region=======MATERIAL ================
            var query = from DataRow row in tbABAUFM.AsEnumerable()
                        where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                        select row;
            foreach (DataRow row in query)
            {
                row["ABAAUFTYP"] = "V";
                row["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
                row["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                row["ABAAUFPOS"] = dwABPN["ABPAUFPOS"];
                row["ABAQTYRESERVE"] = QTYEND*100;
                decimal VKPLAEBIS = System.Convert.ToDecimal(row["ABASSTKLG"].ToString()) * 10;
                if (VKPLAEBIS == 0)
                    VKPLAEBIS = 1;
                decimal ABASGESLG = Convert.ToDecimal(dwABPN["ABPSOLLMENGE2"].ToString()) * 10;
                decimal ABASANZSTK = Math.Ceiling(System.Convert.ToDecimal(ABASGESLG) / VKPLAEBIS);
                decimal ABASSTKLG = 0;

                row["ABASANZSTK"] = System.Math.Ceiling(ABASANZSTK);
                ABASSTKLG = VKPLAEBIS;
                row["ABASSTKLG"] = ABASSTKLG;
                row["ABARESMG"] = QTYEND * 1000;

            }
            //产生下层需求,记录下来本阶段如何计算得出的下层数量。记录在原料层
            DataRow dwMU = tbABAUFM.NewRow();
            dwMU["ABAID"] = ABAID;
            #region=====MapRules=======
            dwMU["QTYEND"] = dwRule["QTYEND"];
            dwMU["QTYMAT"] = dwRule["QTYMAT"];
            dwMU["DELDATE"] = dwRule["DELDATE"];
            dwMU["STARTDATE"] = dwRule["STARTDATE"];
            dwMU["LEADTIME"] = dwRule["LEADTIME"];
            dwMU["UNITNUM"] = dwRule["UNITNUM"];
            dwMU["TTENDS"] = dwRule["TTENDS"];
            dwMU["UENDS"] = dwRule["UENDS"];
            dwMU["UNITQTY"] = dwRule["UNITQTY"];
            dwMU["R01"] = dwRule["R01"];
            dwMU["R01DES"] = dwRule["R01DES"];
            dwMU["R02"] = dwRule["R02"];
            dwMU["R02DES"] = dwRule["R02DES"];
            dwMU["F01"] = dwRule["F01"];
            dwMU["F01DES"] = dwRule["F01DES"];
            dwMU["F02"] = dwRule["F02"];
            dwMU["F02DES"] = dwRule["F02DES"];
            dwMU["JNUM"] = dwRule["JNUM"];
            dwMU["JWASTGE"] = dwRule["JWASTGE"];
            dwMU["JWASTAGEDES"] = dwRule["JWASTAGEDES"];
            dwMU["UMS"] = dwRule["UMS"];
            dwMU["REMARK"] = dwRule["REMARK"];   
            #endregion
            dwMU["ABAMANDANT"] = app.Mandant;
            dwMU["ABAAUFTYP"] = "N";
            dwMU["ABAVKAUFTYP"] = "V";
            dwMU["ABAVKAUFKRS"] = dwABPN["ABPAUFKRS"];
            dwMU["ABAVKAUFLFD"] = dwABPN["ABPAUFLFD"];
            dwMU["ABAVKAUFPOS"] = dwABPN["ABPAUFPOS"];
            dwMU["ABAVKUPOS"] = "01";
            dwMU["ABPWAKZ"] = "F";
            dwMU["ABPART"] = ART;
            dwMU["ABPVAR"] = VAR;
            dwMU["ABPFARBE"] = FARBE;
            dwMU["ABPBESCHICH"] = FINISH;
            dwMU["EXART"] = dwABA["EXART"];
            dwMU["ABAWAKZ"] = "R";
            dwMU["ABAART"] = dwABPN["ABPART"];
            dwMU["ABAVAR"] = dwABPN["ABPVAR"];
            dwMU["ABAFARBE"] = dwABPN["ABPFARBE"];
            dwMU["ABARESMG"] = Convert.ToDouble(dwABPN["ABPSOLLMENGE1"]) * 10;
            dwMU["ABAVKENDD"] = dwABPN["ABPLIEFDAT"];
            dwMU["ABAVKSTARTD"] = dwABPN["ABPSTARTDAT"];        
            dwMU["ABALO"] = PRLO;
            dwMU["ABALB"] = PRLB;
            dwMU["ABALP"] = PRLP;
            dwMU["ABAUSER"] = app.User;
            dwMU["ABADATE"] = date;
            tbABAUFM.Rows.Add(dwMU);
            ABAID++;
            #endregion
            return SReturn;
        }

    }
}